import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../author/author.dart';
import '../home.dart';
import 'widgets/home_widgets.dart';
import '../../../widgets/widgets.dart';
import '../../../shared/shared.dart';

class MeTab extends GetView<HomeLogic> {
  final AuthorLogic c = Get.find();

  MeTab({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Column(
        children: [
          Obx(() => _buildMe()),
          CommonWidget.line(),
          Obx(() => _buildHeader(context)),
          CommonWidget.line(),
          _buildMy(context)
        ],
      ),
    );
  }

  Expanded _buildMy(BuildContext context) {
    return Expanded(child: SingleChildScrollView(child: _buildScroll(context)));
  }

  Column _buildScroll(BuildContext context) {
    return Column(children: const [
      MeAvatar(avatarURL: AssetsURL.headURL),
      MeButton(text: '编辑个人信息'),
      MeTags()
    ]);
  }

  Container _buildMe() {
    return Container(
        margin: const EdgeInsets.all(15),
        child: Center(
            child: Text(c.userName.value,
                style: const TextStyle(
                    color: ColorConstants.themeColor, fontSize: 20))));
  }

  Row _buildHeader(BuildContext context) {
    return Row(children: [
      Container(
        margin: const EdgeInsets.all(10),
        child: Stack(children: [
          const SizedBox(height: 60, width: 75),
          Avatar(imageURL: controller.state.me.value.headURL),
          Obx(() => _buildInfo())
        ]),
      ),
      Expanded(
        child: Row(crossAxisAlignment: CrossAxisAlignment.center, children: [
          const Text('状态',
              style: TextStyle(color: ColorConstants.themeColor, fontSize: 20)),
          CommonWidget.rowWidth(width: 5),
          Text(controller.state.onLine.value ? '在线' : '隐身',
              style: const TextStyle(
                  color: ColorConstants.themeColor, fontSize: 12))
        ]),
      ),
      Container(
          margin: const EdgeInsets.all(10),
          child: CupertinoSwitch(
              activeColor: ColorConstants.selectColor,
              trackColor: ColorConstants.underlineColor,
              onChanged: (value) => controller.changeOnLine(value),
              value: controller.state.onLine.value))
    ]);
  }

  Positioned _buildInfo() {
    return Positioned(
      right: 0,
      bottom: 0,
      child: Container(
          width: 24,
          height: 10,
          decoration: BoxDecoration(
              border: Border.all(color: Colors.white, width: 1),
              color: controller.state.onLine.value
                  ? ColorConstants.selectColor
                  : ColorConstants.underlineColor,
              borderRadius: const BorderRadius.all(Radius.circular(6)))),
    );
  }
}
